package com.tan.websocket.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tan.common.domain.chat.ChatGroup;
import com.tan.common.domain.chat.ChatInfo;
import com.tan.common.dto.chat.ChatInfoDTO;
import com.tan.common.enums.MessageTypeEnum;
import com.tan.core.utils.ThreadPoolTool;
import com.tan.websocket.event.ChatGroupMember;
import com.tan.websocket.event.ChatPublisher;
import com.tan.websocket.service.IChatInfoService;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;


/**
 * @author Tan
 * @description
 * @date 2025/1/26
 */
@Slf4j
@Service
public class FrameTextService extends MainFrameAbstractService {

    @Resource
    private ChatBaseHandle chatBaseHandle;

    @Override
    public void handleMessage(ChannelHandlerContext ctx, WebSocketFrame frame) {
        TextWebSocketFrame textFrame = (TextWebSocketFrame) frame;
        String text = textFrame.text();
        log.info("收到文本消息：：{}", text);
        ChatInfoDTO chatInfoDTO = JSONObject.parseObject(text, ChatInfoDTO.class);
        // 获取消息接收者通道信息 todo 这里有点问题 需要再设计一下 如果是群消息
        chatBaseHandle.handle(chatInfoDTO);
        super.createChatInfo(chatInfoDTO);
    }

    @Override
    public boolean isFrameType(WebSocketFrame frame) {
        return frame instanceof TextWebSocketFrame;
    }


    @Override
    public String serviceName() {
        return TextWebSocketFrame.class.getSimpleName();
    }
}
